package com.erp.app.common.VUtil.sql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class OracleTableStructureDumper extends ATableStructureDumper {
    public OracleTableStructureDumper(Connection conn) throws SQLException {
        super(conn);
    }

    public ResultSet getIndexInfo(ResultSet rs) throws SQLException {
        super.getIndexInfo(rs);
        try {
            sSchema = rs.getString(2);
        } catch (SQLException xx) {
        }
        return null;
    }

    public boolean isEffectiveRow(ResultSet rs) throws SQLException {
        try {
            rs.getObject(1);
        } catch (Exception x) {
            return false;
        }
        return true;
    }

    public String getConstraintLine(String sTable) {
        return "CONSTRAINT " + "PK_" + sTable + " PRIMARY KEY ";
    }

    public String getColumns(ResultSetMetaData md, String sTable) throws SQLException {
        StringBuffer sb = new StringBuffer();
        HashMap hm = new HashMap();
        ResultSet rs;
        rs = conn.createStatement().executeQuery(
                "select column_name, data_type, data_precision, data_length, data_scale from all_tab_columns\n"
                        + "where table_name = '" + sTable + "' and owner = user");
        if (!rs.next())
            throw new SQLException("BBA: Table name '" + sTable + "' not found in table all_tab_columns. Try to use '"
                    + sTable.toUpperCase() + "'.");
        do {
            Vector v = new Vector();
            for (int i = 2; i < 6; i++)
                v.add(rs.getObject(i));
            hm.put(rs.getString(1), v.toArray());
        } while (rs.next());
        sb.append("    " + getLine(md, 1, hm));
        for (int j = 2; j <= md.getColumnCount(); j++)
            sb.append(",\r\n    " + getLine(md, j, hm));
        return sb.toString();
    }

    public String getLine(ResultSetMetaData md, int iRow, Map m) throws SQLException {
        String key = md.getColumnName(iRow);
        return key + "    " + getColumnTypeName(md, iRow, (Object[]) m.get(key))
                + (md.columnNoNulls == md.isNullable(iRow) ? " NOT" : "") + " NULL";
    }

    public String getColumnTypeName(ResultSetMetaData md, int i, Object[] objs) throws SQLException {
        String typeName = (String) objs[0];

        boolean bDisplay = true, bScale = false;
        switch (md.getColumnType(i)) {
            case Types.LONGVARBINARY:
            case Types.INTEGER:
            case Types.SMALLINT:
            case Types.TIMESTAMP:
                bDisplay = false;
                break;
            case Types.DECIMAL:
            case Types.NUMERIC:
                bScale = true;
                break;
            default:
        // System.out.println("type = " + i );
        }
        return typeName + "    " + (bDisplay ? "(" + (bScale ? objs[1] : objs[2]) + (bScale ? "," + objs[3] : "") + ")" : "");
    }
}
